package com.magicborrow.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.magicborrow.R;
import com.magicborrow.activity.WantBorrowingInfoActivity;
import com.magicborrow.beans.StuffBean;
import com.magicborrow.beans.WantBorringListBean;

import java.util.ArrayList;

/**
 * Created by ghost on 16/3/28.
 */
public class PublishedBorrowAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

    private static final int TYPE_FOOT = 0;
    private static final int TYPE_NORMAL = 1;
    private ArrayList<WantBorringListBean.DataEntity.WantBorrowingBean> mData;
    private Context mContext;
    private LayoutInflater mLayoutInflater;

    public PublishedBorrowAdapter(Context pContext){
        this.mContext = pContext;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setData(ArrayList<WantBorringListBean.DataEntity.WantBorrowingBean> pData){
        this.mData = pData;
    }

    public ArrayList<WantBorringListBean.DataEntity.WantBorrowingBean> getData(){
        return  this.mData;
    }

    @Override
    public int getItemViewType(int position) {
        if(position+1 == getItemCount()){
            return TYPE_FOOT;
        }else{
            return TYPE_NORMAL;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_FOOT){
            View view = mLayoutInflater.inflate(R.layout.load_more_footer,null);
            view.setTag("foot");
            return new FootViewHolder(view);
        }else if(viewType == TYPE_NORMAL){
            View view = mLayoutInflater.inflate(R.layout.published_borrow_item,null);
            return new PublishedBorrowViewHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if(holder instanceof  PublishedBorrowViewHolder){
            PublishedBorrowViewHolder hold = (PublishedBorrowViewHolder)holder;
            final WantBorringListBean.DataEntity.WantBorrowingBean bean = mData.get(position);
            hold.tvName.setText(bean.getName());
            hold.saveTime.setText("时间: "+bean.getCreated());
            hold.describle.setText(bean.getDescription());
            hold.wantborrowday.setText(bean.getNumber()+" ");
            hold.location.setText(bean.getAddress());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("stuff",bean);
                    intent.putExtra("canEdit",true);

                    intent.setClass(mContext, WantBorrowingInfoActivity.class);
                    mContext.startActivity(intent);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mData.size()+1;
    }



    public class PublishedBorrowViewHolder extends RecyclerView.ViewHolder {

        private TextView tvName;
        private TextView saveTime;
        private TextView describle;
        private TextView wantborrowday;
        private TextView location;
        private ImageView edit;

        public PublishedBorrowViewHolder(View view) {
            super(view);
            tvName = (TextView) view.findViewById(R.id.tv_name);
            saveTime = (TextView)view.findViewById(R.id.time);
            describle = (TextView)view.findViewById(R.id.describle);
            wantborrowday = (TextView)view.findViewById(R.id.wantborrowday);
            location = (TextView)view.findViewById(R.id.location);
            edit = (ImageView) view.findViewById(R.id.edit);
        }
    }

    public class FootViewHolder extends RecyclerView.ViewHolder{

        public FootViewHolder(View itemView) {
            super(itemView);
        }

    }

}
